<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>航班添加页面</title>
</head>
<body>

<h1>航班添加页面</h1>
<form>
    <p>
        航班编号：<input type="text" id="flightNo"/><span id="msg"></span>
    </p>
    <p>
        起飞城市：<select id="departureCity"></select>&nbsp;&nbsp;
        起飞时间：<input type="text" id="departureTime"/>
    </p>
    <p>
        到达城市：<select id="arrivalCity"></select>&nbsp;&nbsp;
        到达时间：<input type="text" id="arrivalTime"/>
    </p>
    <p>
        <input type="button" value="添加" id="addBtn"/>&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="button" value="返回首页" onclick="location='index.html'"/>
    </p>
</form>

<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script>
    $(function () {
        $.ajax({
            url: "flight/showCity",
            type: "get",
            data: "",
            dataType: "json",
            success: function (data) {
                var opt1 = "<option value='0' checked>--请选择起飞城市--</option>";
                var opt2 = "<option value='0' checked>--请选择到达城市--</option>";
                $.each(data, function () {
                    opt1 += "<option value='" + this.id + "'>" + this.cityName + "</option>";
                    opt2 += "<option value='" + this.id + "'>" + this.cityName + "</option>";
                });
                $("#departureCity").html(opt1);
                $("#arrivalCity").html(opt2);
            }
        });

        $("#flightNo").blur(function () {
            var flightNo = $("#flightNo").val();
            if (flightNo == "") {
                $("#msg").html("");
                return;
            } else {
                $.ajax({
                    url: "flight/count",
                    type: "post",
                    data: {"flightNo": flightNo},
                    dataType: "json",
                    success: function (data) {
                        if (data == true) {
                            $("#msg").css("color", "red").html("航班编号以存在，请重新输入！");
                        } else {
                            $("#msg").css("color", "chartreuse").html("航班编号可用！");
                        }
                    }
                });
            }
        });

        $("#addBtn").click(function () {
            var flightNo = $("#flightNo").val();
            var departureCity = $("#departureCity").val();
            var arrivalCity = $("#arrivalCity").val();
            var departureTime = $("#departureTime").val();
            var arrivalTime = $("#arrivalTime").val();
            if (flightNo == "") {
                alert("请填写航班编号！");
                return;
            }
            if (departureCity == 0) {
                alert("请选择起飞城市！");
                return;
            }
            if (arrivalCity == 0) {
                alert("请选择到达城市！");
                return;
            }
            if (departureCity == arrivalCity) {
                alert("起飞城市和到达城市不能相同！");
                return;
            }
            var dateType = /^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\s([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;
            if (!dateType.exec(departureTime)) {
                alert("起飞时间格式错误！");
                return;
            }
            if (!dateType.exec(arrivalTime)) {
                alert("到达时间格式错误！");
                return;
            }
            var params = {
                "flightNo": flightNo,
                "departureCity": departureCity,
                "arrivalCity": arrivalCity,
                "departureTime": departureTime,
                "arrivalTime": arrivalTime
            };

            $.ajax({
                url: "flight/add",
                type: "post",
                data: JSON.stringify(params),
                dataType: "json",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Content-type", "application/json");
                },
                success: function (data) {
                    if (data == true) {
                        alert("添加成功！");
                    } else {
                        alert("添加失败！");
                    }
                }
            });
        });

    });

</script>
</body>
</html>